<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Weather;
use App\Server\Constants;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WeatherController extends AdminController
{

    public function index(Content $content)
    {
        return $content
            ->header('天气')
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Weather(), function (Grid $grid) {
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand();
                $filter->panel();
                $filter->equal('city', '区县')->select(Constants::$city)->width(3);
                $filter->date('date', '日期')->width(3);
            });

            $grid->model()->orderByDesc('id');

            $grid->showActions();
            $grid->disableRowSelector();
            $grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableViewButton();

            $grid->column('id')->sortable();
            $grid->column('city_name');
            $grid->column('date')->sortable();
            $grid->column('weather');
            $grid->column('temperature');
            $grid->column('wind_direction');
            $grid->column('wind_power');
            $grid->column('pressure');
            $grid->column('score');
            $grid->column('evaluate')->display(function ($value) {
                if ($this->bg_color == 'warning') {
                    return "<span class='label' style='background:#21b978'>{$value}</span>";
                }
                if ($this->bg_color == 'warning') {
                    return "<span class='label' style='background:#dda451'>{$value}</span>";
                }
                if ($this->bg_color == 'danger') {
                    return "<span class='label' style='background:#ea5455'>{$value}</span>";
                }
                return '';
            });
            $grid->column('proposal')->limit(30);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->scrollbarX();
            $grid->simplePaginate();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Weather(), function (Form $form) {
            $form->disableViewButton();
            $form->display('id');
            $form->text('city')->readOnly();
            $form->text('city_name')->readOnly();
            $form->text('weather');
            $form->text('temperature');
            $form->text('evaluate');
            $form->text('proposal');
            $form->text('wind_direction');
            $form->text('wind_power');
            $form->text('pressure');
            $form->text('score');
            $form->text('bg_color')->readOnly();
            $form->text('date')->readOnly();
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
